package org.bilinc.gz001;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.util.Enumeration;
import java.util.Vector;

class ColumnGroup {
    private final TableCellRenderer mColumnRenderer;
    private final Vector mColumnTitles = new Vector();
    private final String mHeader;
    private int mMargin = 0;

    public ColumnGroup(final String text) {
        this(null, text);
    }

    public ColumnGroup(final TableCellRenderer renderer,final String text) {
        if (renderer == null) {
            mColumnRenderer = new DefaultTableCellRenderer() {
                public Component getTableCellRendererComponent(JTable table,
                                                               Object value,
                                                               boolean isSelected,
                                                               boolean hasFocus,
                                                               int row,
                                                               int column) {
                    JTableHeader header = table.getTableHeader();
                    if (header != null) {
                        setForeground(header.getForeground());
                        setBackground(header.getBackground());
                        setFont(header.getFont());
                    }
                    setHorizontalAlignment(JLabel.CENTER);
                    setText((value == null) ? "" : value.toString());
                    setBorder(UIManager.getBorder("TableHeader.cellBorder"));
                    return this;
                }
            };
        } else {
            mColumnRenderer = renderer;
        }
        mHeader = text;
    }

    public void add(final Object obj) {
        if (obj == null) {
            return;
        }
        mColumnTitles.addElement(obj);
    }

    public Vector getColumnGroups(final TableColumn c, final Vector g) {
        g.addElement(this);
        if (mColumnTitles.contains(c)) return g;
        Enumeration e = mColumnTitles.elements();
        while (e.hasMoreElements()) {
            Object obj = e.nextElement();
            if (obj instanceof ColumnGroup) {
                Vector groups =
                        (Vector) ((ColumnGroup) obj).getColumnGroups(c, (Vector) g.clone());

                if (groups != null) {
                    return groups;
                }
            }
        }
        return null;
    }

    public TableCellRenderer getHeaderRenderer() {
        return mColumnRenderer;
    }

    public Object getHeaderValue() {
        return mHeader;
    }

    public Dimension getSize(final JTable table) {
        Component comp = mColumnRenderer.getTableCellRendererComponent(
                table, getHeaderValue(), false, false, -1, -1);
        int height = comp.getPreferredSize().height;
        int width = 0;
        Enumeration e = mColumnTitles.elements();
        while (e.hasMoreElements()) {
            Object obj = e.nextElement();
            if (obj instanceof TableColumn) {
                TableColumn aColumn = (TableColumn) obj;
                width += aColumn.getWidth();
            } else {
                width += ((ColumnGroup) obj).getSize(table).width;
            }
        }
        return new Dimension(width, height);
    }

    public void setColumnMargin(final int margin) {
        mMargin = margin;
        Enumeration e = mColumnTitles.elements();
        while (e.hasMoreElements()) {
            Object obj = e.nextElement();
            if (obj instanceof ColumnGroup) {
                ((ColumnGroup) obj).setColumnMargin(margin);
            }
        }
    }
}